package com.lagou.edu.wwyan.web1.minicat;

import java.io.*;

public class Response {
    /**
     * 输出流
     */
    private OutputStream outputStream;
    /**
     * 相应状态
     */
    private int status = 200;

    public Response(OutputStream outputStream) {
        this.outputStream = outputStream;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public OutputStream getOutputStream() {
        return outputStream;
    }

    public void setOutputStream(OutputStream outputStream) {
        this.outputStream = outputStream;
    }

    public void output(String content) throws IOException {
        this.outputStream.write(content.getBytes());
    }

    /**
     * 输出静态资源
     * @param path
     */
    public void outputStatic(String path) throws IOException {
        String absolutePath = StaticResourceUtil.getAbsolutePath(path);
        outputAbsolutePath(absolutePath);


    }

    public void outputAbsolutePath(String absolutePath) throws IOException {
        File file = new File(absolutePath);
        if (file.exists() && file.isFile()){
            StaticResourceUtil.outputStaticResource(new FileInputStream(file),outputStream);
        }else {
            output(HttpProtocolUtil.getHttpResult404());
        }
    }


}
